package com.wy.test.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.wy.entity.User;

public class Generic2 {

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	public Generic2(){
		
	}
	
	public static void main(String args[]){
		Generic2 test = new Generic2();
		User u = new User();
		u.setId(2);
		u.setUsername("wy");
		u.setPassword("pass");
		Map<String, Object> map = test.t2Map(u);
		if(map !=null && !map.isEmpty()){
			for(Map.Entry<String, Object> entry : map.entrySet()){
				String key = entry.getKey();
				String value = entry.getValue().toString();
				System.out.println(key +"--->"+ value);
			}
		}
		
		//map2T test
		User convert = test.map2T(User.class, map);
		if(convert != null){
			System.out.println(convert.getId() +","+ convert.getUsername() +","+ convert.getPassword());
			System.out.println((convert.getId() == 2));
		}
		
	}
	
	/**
	 * Map对象赋值给T对象
	 * @param map
	 * @return
	 */
	public <T> T map2T(Class<T> cz, Map<String,Object> map){
		if(map.isEmpty()){
			return null;
		}
		T t = null;
		try {
			t = cz.newInstance();
			Method[] ms = t.getClass().getDeclaredMethods();
			for(Method m : ms){
				String mName = m.getName();
				if(mName.contains("set")
						&& !mName.equals("equals")
						&& !mName.equals("toString")
						&& !mName.equals("hashCode")){
					String key = firstToLowercase(mName.substring(3));
					m.invoke(t, map.get(key));
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return t;
	}
	
	/**
	 * T对象赋值给Map对象
	 * @param t 泛型对象
	 * @return Map
	 */
	public <T> Map<String, Object> t2Map(T t){
		if(t == null){
			log.warning("this income object is null");
			return null;
		}
		
		Map<String, Object> map = null;
		try{
			map = new HashMap<String, Object>();
			Class<?> c = t.getClass();
			Method[] methods = c.getDeclaredMethods();
			for(Method m : methods){
				String mName = m.getName();
				if(mName.contains("get") 
						&& !mName.equals("equals")
						&& !mName.equals("toString")
						&& !mName.equals("hashCode")){
					String key   = firstToLowercase(mName.substring(3));
					Object value = m.invoke(t);
					map.put(key, value);
				}
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 首字母变成小写
	 * @param str
	 * @return
	 */
	public String firstToLowercase(String str){
		if(str == null || str.equals("")){
			return null;
		}
		String s = str.substring(0, 1);
		return str.replace(s, s.toLowerCase());
	}
	
	/**
	 * 遍历对象属性
	 * @param t
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings("rawtypes")
	public <T> List<T> getClassByT(T t) throws InstantiationException, IllegalAccessException{
		Class c = t.getClass();
		Field[] fields = c.getDeclaredFields();
		String fieldName,fieldValue;
		
		System.out.println(c.getName());
		System.out.println(fields.length);
		for(Field f : fields){
			fieldName = f.getName();
//			fieldValue = f.get(fieldName).toString();
			System.out.println(fieldName +"--->");
		}
		return null;
	}
}
